﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using System.Data;
using System.Data.SqlClient;
using Support.DTO;

namespace DataAccess
{
    public class EvolucionClinicaDAL : BaseSQLDAL
    {
        public List<EvolucionClinicaDTO> ObtenerPorHistoriaId(int historiaId)
        {
            var lista = new List<EvolucionClinicaDTO>();

            try
            {

                CreateCommand("EvolucionClinica_ObtenerPorHistoriaId", true);
                AddCmdParameter("@HistoriaId", historiaId, ParameterDirection.Input);

                ExecuteReader();
                while (Read())
                {
                    lista.Add(FillDTO(GetDataReader()));
                }
                return lista;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                DisposeDataReader();
                CloseConnection();
            }
        }

        public void EliminarPorHistoriaId(int historiaId)
        {
            try
            {

                CreateCommand("EvolucionClinica_EliminarPorHistoriaId", true);
                AddCmdParameter("@HistoriaId", historiaId, ParameterDirection.Input);

                ExecuteReader();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                DisposeDataReader();
                CloseConnection();
            }
        }

        private EvolucionClinicaDTO FillDTO(SqlDataReader reader)
        {
            return new EvolucionClinicaDTO()
            {
                Id = Convert.ToInt32(GetOutResult("evolucionId")),
                Fecha = Convert.ToDateTime(GetOutResult("fecha")),
                Contenido = Convert.ToString(GetOutResult("contenido")),
                HistoriaId = Convert.ToInt32(GetOutResult("historiaId")),
                Guardada = true
            };
        }

        public int Guardar(EvolucionClinicaDTO evolucion)
        {
            try
            {
                connection = new SqlConnection(Support.Configuration.ConfigurationManager.ConnectionString_Sindicato);
                CreateCommand("EvolucionClinica_Guardar", true);
                AddCmdParameter("@Fecha", evolucion.Fecha, ParameterDirection.Input);
                AddCmdParameter("@Contenido", evolucion.Contenido, ParameterDirection.Input);
                AddCmdParameter("@HistoriaId", evolucion.HistoriaId, ParameterDirection.Input);

                ExecuteReader();
                Read();

                return Convert.ToInt32(GetOutResult("evolucion_id"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                //DisposeDataReader();
                CloseConnection();
            }
        }
    }
}
